package hydrap2p.search;


public class ListenerSearchSyncerator implements Runnable {

	private ListenerIface listener;
	private Search search;
	public ListenerSearchSyncerator(ListenerIface listener, Search search)
	{
		this.listener=listener;
		this.search=new Search(search);
	}

	@Override
	public void run() {
		try {
			// figure out the providers that have finished already
			for (String provider : search.getSearchActiveProviders())
				listener.searchProviderStarted(search.getSearchId(), provider);
			if (search.getSearchActiveProviders().size()==0)
				listener.searchFinished(search.getSearchId());
			for (SearchResult result:search.getSearchResults())
			{
				listener.addSearchResult(search.getSearchId(), result);
			}
		}
		catch (Exception e) {
			// Failed to notify the listener, he must've died
			return;
		}
	}
}
